<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2018/5/30
 * Time: 下午1:47
 */

namespace App\Modules\Shop\Services;


use App\Modules\Order\Models\Order;
use App\Modules\Shop\Models\OrderProducts;
use App\Modules\Shop\Models\Products;

class OrderProductsService
{
    const ORDER_STATUS_STR_TO_NUM_HASH = [
        'unpaid' => [0,2],
        /*'paid' =>1,
        'failed' => 2,
        'refund' => 3,
        'prepare'=>4,*/
        'undeliver'=>[1,4],
        'delivered'=>5,
        //'received' =>7
    ];

    const ORDER_STATUS_NUM_TO_STR_HASH = [
        '0'=>'unpaid',
        '1'=>'undeliver',
        '2'=>'unpaid',
        '4'=>'undeliver',
        '5'=>'delivered',
        '6'=>'received',
        0=>'unpaid',
        1=>'undeliver',
        2=>'unpaid',
        4=>'undeliver',
        5=>'delivered',
        6=>'received',

    ];

    public static function insertMuti($data) {
        return OrderProducts::insert($data);
    }

    public static function getById($id) {
        return OrderProducts::find($id);
    }

    public static function getUserOrderList($uid) {
        return OrderProducts::where('uid',$uid)->orderBy('id','desc')->paginate();
    }

    public static function addOne($data) {
        return OrderProducts::create($data);
    }

    public static function getUserSubOrderList($uid,$filters,$is_all=false) {
        $obj = OrderProducts::join('orders','orders.id','=','order_products.id')
            ->join('products','products.id','=','order_products.product_id')
            ->select(['order_products.id','products.product_name','products.img_thum','products.desc','order_products.price','order_products.count','order_products.discount','order_products.amount','order_products.status'])
            ->where('orders.uid',$uid)->orderBy('order_products.id','desc');
        if(isset($filters['status'])) {
            if(is_array($filters['status'])) {
                $obj->whereIn('order_products.status',$filters['status']);
            }else{
                $obj->where('order_products.status',$filters['status']);
            }

        }
        if($is_all==true) {
            return $obj->get();
        }
        return $obj->paginate();
    }

    /**
     * @param $uid
     * @param $filters
     * @param bool $is_all
     * @return mixed
     */
    public static function getUserOrderAndSubUnionList($uid,$filters,$is_all=false) {

        if(!isset($filters['status']) || (isset($filters['status']) && empty($filters['status']))) {
            $order_query = Order::where('uid',$uid)->whereIn('status',self::ORDER_STATUS_STR_TO_NUM_HASH['unpaid'])->select(\DB::raw('id,trade_no,status,created_at,0 as count,0 as price, 0 as amount, 0 as discount,0 as img_thum,0 as product_name,0 as specification,ship_fee,1 as type'))->orderBy('id','desc');

            $sub = OrderProducts::join('products','products.id','=','order_products.product_id')
                ->join('product_specification','product_specification.id','=','order_products.product_spec_id')
                ->where('uid',$uid)->whereNotIn('order_products.status',self::ORDER_STATUS_STR_TO_NUM_HASH['unpaid'])
                ->select(\DB::raw('in_order_products.id,in_order_products.sub_trade_no as trade_no,in_order_products.status,in_order_products.created_at,in_order_products.count,in_order_products.price,in_order_products.amount,in_order_products.discount,in_products.img_thum,in_products.product_name,in_product_specification.specification,ship_fee,2 as type'))
                ->orderBy('created_at','desc')
                ->union($order_query);

            $obj = \DB::table( \DB::raw("({$sub->toSql()}) as sub") )
                ->mergeBindings($sub->getQuery())->orderBy('created_at','desc');
            //\Log::info($obj->toSql());
            //return ;
        }else if(isset($filters['status']) && $filters['status']=='unpaid') {
            $obj = Order::where('uid',$uid)->whereIn('status',self::ORDER_STATUS_STR_TO_NUM_HASH['unpaid'])
                ->select(\DB::raw('id,trade_no,status,created_at,0 as count,0 as price, 0 as amount, 0 as discount,0 as img_thum,0 as sub_trade_no,0 as product_name,0 as specification,ship_fee,1 as type'))
                ->orderBy('created_at','desc');
        }else{
            $obj = OrderProducts::join('products','products.id','=','order_products.product_id')
                ->join('product_specification','product_specification.id','=','order_products.product_spec_id')
                ->where('uid',$uid)
                ->select(\DB::raw('in_order_products.id,in_order_products.sub_trade_no as trade_no,in_order_products.status,in_order_products.created_at,in_order_products.count,in_order_products.price,in_order_products.amount,in_order_products.discount,in_products.img_thum,in_products.product_name,in_product_specification.specification,ship_fee,2 as type'))
                ->orderBy('created_at','desc');

            $status = self::ORDER_STATUS_STR_TO_NUM_HASH[$filters['status']];
            if(is_array($status)) {
                $obj->whereIn('order_products.status',$status);
            }else{
                $obj->where('order_products.status',$status);
            }
        }

        if($is_all){
            return $obj->get();
        }
        return $obj->paginate();
    }

    public static function getOrderProductsInfoByOrderId($order_id) {
        return OrderProducts::join('products','products.id','=','order_products.product_id')
            ->join('product_specification','product_specification.id','=','order_products.product_spec_id')
            ->where('order_id',$order_id)
            ->select(\DB::raw('in_order_products.id,sub_trade_no as trade_no,in_order_products.status,in_order_products.created_at,in_order_products.count,in_order_products.price,in_order_products.amount,in_order_products.discount,in_products.img_thum,in_products.product_name,in_product_specification.specification,ship_fee'))
            ->get();
    }
}